﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Microsoft.Kinect;
using Coding4Fun.Kinect.Wpf;
using System.Runtime.InteropServices;
using System.Windows;
using Kinect_Interface_Prototype_1.Gesture_Control;


namespace Gesture_Control.Keyboard
{
    public partial class KeyboardCover : Form
    {
        [DllImport("user32.dll")]
        static extern void mouse_event(int dwFlags, uint dx, int dy, int dwData, int dwExtraInfo);

        // constants for the mouse_input() API function
        private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        private const int MOUSEEVENTF_LEFTUP = 0x0004;
        private bool leftTimer;
        private bool rightTimer;
        private int leftTime = 0;
        private int rightTime = 0;
        double screenWidth = SystemParameters.PrimaryScreenWidth;
        double screenHeight = SystemParameters.PrimaryScreenHeight;
        private static System.Windows.Point lefthandPoint = new System.Windows.Point();
        private static System.Windows.Point righthandPoint = new System.Windows.Point();
        //double conversionX = .1875;
        //double conversionY = .6444;
        float rtmpz;
        float ltmpz;
        float lxp = 0;
        float lyp = 0;
        float lzp = 0;
        float rxp = 0;
        float ryp = 0;
        float rzp = 0;
        int canTypeLeft =0;
        int canTypeRight = 0;
        VKeyboard.KeyboardHolder vk = new VKeyboard.KeyboardHolder();

        public KeyboardCover()
        {
            InitializeComponent();
            
        }

        private void KeyboardCover_Load(object sender, EventArgs e)
        {
            vk.Show();
            
            this.Location = new System.Drawing.Point((Screen.PrimaryScreen.WorkingArea.Width / 2) -
           ((this.Width) / 2), Screen.PrimaryScreen.WorkingArea.Height - this.Height);
            
            
            this.BringToFront();
            
        }

        public void setLeftHandPosition(Joint leftHand)
        {
            checkTimer();
            if (leftTimer)
            {
                leftTime++;
            }

            if (leftTime > 30)
            {
                canTypeLeft = 0;
                ltmpz = lzp;
                leftTime = 0;
                this.BringToFront();

            }
            Graphics g = Kinect_Interface_Prototype_1.StartupProgram.VirtualKeyboard.CreateGraphics();
            g.Clear(Color.Silver);
            Joint scaledleft = leftHand.ScaleTo(950, 300, 0.50f, 0.15f);
            lxp = Convert.ToSingle(scaledleft.Position.X);
            lyp = Convert.ToSingle(scaledleft.Position.Y);
            lzp = Convert.ToSingle(scaledleft.Position.Z) * 1000;
            if ((ltmpz - lzp) > 60 && leftTime > 20 && canTypeLeft == 0)
            {
                click(scaledleft);
                canTypeLeft = 1;
            }


            SolidBrush color = new SolidBrush(Color.Black);
            g.FillEllipse(color, lxp, lyp, 10, 10);



        }

        private void checkTimer()
        {
            if (leftTimer == false)
            {
                leftTimer = true;
            }

            if (rightTimer == false)
            {
                rightTimer = true;
            }
                
        }

        

        public void setRightHandPosition(Joint rightHand)
        {
            checkTimer();

            if (rightTimer)
            {
                rightTime++;
            }

            if (rightTime > 30)
            {
                canTypeRight = 0;
                rtmpz = rzp;
                rightTime = 0;
                Kinect_Interface_Prototype_1.StartupProgram.VirtualKeyboard.BringToFront();

            }

            Graphics g = Kinect_Interface_Prototype_1.StartupProgram.VirtualKeyboard.CreateGraphics();
            Joint scaledRight = rightHand.ScaleTo(875, 300, 0.50f, 0.15f);
            rxp = Convert.ToSingle(scaledRight.Position.X);
            ryp = Convert.ToSingle(scaledRight.Position.Y);
            rzp = Convert.ToSingle(scaledRight.Position.Z) * 1000;
            if ((rtmpz - rzp) > 60 && rightTime > 20 && canTypeRight == 0)
            {
                click(scaledRight);
                canTypeRight = 1;
            }

            SolidBrush color = new SolidBrush(Color.Black);
            g.FillEllipse(color, rxp, ryp, 10, 10);



        }

        private void click(Joint scaledHand)
        {
            //Need a formula to automatically adjust to resolution
            //Currently use 270 and 560 for 1440x900 and 65 and 425 for 1024 x 768


           // int adjustmentX = convertDoubleToInt(conversionX * screenWidth);
            //int adjustmentY = convertDoubleToInt(conversionY * screenHeight);
            Cursor.Position = new System.Drawing.Point(Convert.ToInt32((scaledHand.Position.X) + 65), Convert.ToInt32((scaledHand.Position.Y)) + 425);
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            Kinect_Interface_Prototype_1.StartupProgram.VirtualKeyboard.BringToFront();

        }

        public System.Windows.Point getLeftHandPosition(System.Windows.Point lHandPoint)
        {
            return lefthandPoint;

        }

        public System.Windows.Point getRightHandPosition(System.Windows.Point rHandPoint)
        {
            return righthandPoint;

        }

        public int convertDoubleToInt(double conversionNum)
        {
            return Convert.ToInt32(conversionNum);
        }


        public void ShowKeyboard()
        {
            this.Show();
            vk.Show();
        }

        public void HideKeyboard()
        {
            this.Hide();
            vk.Hide();
        }
    }
}
